#!/bin/bash

# Slackware build script for ostrichriders

# Written by B. Watson (urchlay@slackware.uk)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# 20211020 bkw: BUILD=2, add old-style icon
# 20191130 bkw: update for v0.6.5, update man page to add new -a option

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=ostrichriders
VERSION=${VERSION:-0.6.5}
BUILD=${BUILD:-2}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tgz
cd $PRGNAM-$VERSION
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

# This stuff looks like it was included in the tarball by mistake.
rm -f data/mods/sandbox/media/Thumbs.db \
      data/mods/*/"scores (copie).dat"

# SBo's SFML doesn't ship a .pc file for pkg-config, so do it the hard
# way. -Wl,-s strips the binary.

sed -i 's,Exec=,&/usr/games/,' $PRGNAM.desktop

make PREFIX=/usr \
     BINDIR=/usr/games \
     DESTDIR=$PKG \
     ADD_CFLAGS="$SLKCFLAGS" \
     ADD_LDFLAGS="-Wl,-s \
                  -L/usr/lib$LIBDIRSUFFIX \
                  -lsfml-audio \
                  -lsfml-graphics \
                  -lsfml-system \
                  -lsfml-window \
                  $( pkg-config --libs fontconfig )" \
     all \
     install

# 20211020 bkw: old-style icon.
mkdir -p $PKG/usr/share/pixmaps
ln -s ../icons/hicolor/48x48/apps/$PRGNAM.png $PKG/usr/share/pixmaps/$PRGNAM.png

# man page written for this build (if nothing else, I need the practice).
mkdir -p $PKG/usr/man/man6
gzip -9c < $CWD/$PRGNAM.6 > $PKG/usr/man/man6/$PRGNAM.6.gz

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a LICENCE README html/* $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
